package com.alibaba.citrus.cr.sales.plan.facade.service.salesplan.audit;

import com.alibaba.citrus.cr.common.annotation.FacadeInvoker;
import com.alibaba.citrus.cr.common.util.DateUtils;
import com.alibaba.citrus.cr.sales.plan.api.salesplan.audit.SalesPlanAuditTaskQueryService;
import com.alibaba.citrus.cr.sales.plan.dto.api.request.SalesPlanAuditTaskQueryRequest;
import com.alibaba.citrus.cr.sales.plan.repository.SalesPlanAuditTaskRepository;
import com.alibaba.citrus.cr.sales.plan.sdo.SalesPlanAuditTaskSDO;
import com.alibaba.cz.base.tool.StringUtil;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author:suqing.tjf
 * @date:2021/9/16
 */
@Service
public class SalesPlanAuditTaskQueryServiceImpl implements SalesPlanAuditTaskQueryService {

    @Resource
    private SalesPlanAuditTaskRepository salesPlanAuditTaskRepository;

    @Override
    @FacadeInvoker
    public Result<PageResult<List<SalesPlanAuditTaskSDO>>> pageQuerySalesPlanAuditTask(
        SalesPlanAuditTaskQueryRequest request) {
        PageResult<List<SalesPlanAuditTaskSDO>> listPageResult = salesPlanAuditTaskRepository.pageQuery(request);
        refreshResult(listPageResult);
        return Result.success(listPageResult);
    }

    private void refreshResult(PageResult<List<SalesPlanAuditTaskSDO>> listPageResult) {
        if (listPageResult == null || CollectionUtils.isEmpty(listPageResult.getContent())) {
            return;
        }
        listPageResult.getContent().forEach(salesPlanAuditTaskSDO -> {
            if (StringUtil.isNotEmpty(salesPlanAuditTaskSDO.getPlanTime())) {
                //返回数据是时间戳,对时间戳做处理
                String planTime = DateUtils.stampToDate(salesPlanAuditTaskSDO.getPlanTime());
                try {
                    salesPlanAuditTaskSDO.setPlanTime(DateUtils.formatPlanTime(planTime));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
